package org.dtrd.modules.evaluation.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.evaluation.constant.Evaluation;
import org.dtrd.modules.evaluation.entity.bean.nrs.NrsDetail;
import org.dtrd.modules.evaluation.entity.bean.nrs.NrsListInfo;
import org.dtrd.modules.evaluation.entity.bean.nrs.NrsListRequest;
import org.dtrd.modules.evaluation.entity.bean.nrs.NrsPageRequest;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdNrs;
import org.dtrd.modules.evaluation.mapper.DtrdEntEvaluationRdNrsMapper;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdNrsService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xiao
 * @since 2022-08-15
 */
@DS("multi-datasource1")
@Service
public class DtrdEntEvaluationRdNrsServiceImpl extends ServiceImpl<DtrdEntEvaluationRdNrsMapper, DtrdEntEvaluationRdNrs> implements IDtrdEntEvaluationRdNrsService {

    private DtrdEntEvaluationRdNrsMapper dtrdEntEvaluationRdNrsMapper;

    @Override
    public List<NrsListInfo> getNrsList(Integer patientId) {
        LambdaQueryWrapper<DtrdEntEvaluationRdNrs> queryWrapper = baseQueryWrapper()
                .eq(DtrdEntEvaluationRdNrs::getPatientId, patientId)
                .orderByDesc(DtrdEntEvaluationRdNrs::getCreateTime);
        List<DtrdEntEvaluationRdNrs> poList = list(queryWrapper);
        List<NrsListInfo> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream().map(NrsListInfo::new).collect(Collectors.toList());
        }
        return dtoList;
    }

//    @Override
//    public IPage<NrsListInfo> pageNrs(NrsPageRequest request) {
//        LambdaQueryWrapper<DtrdEntEvaluationRdNrs> queryWrapper = baseQueryWrapper();
//        IPage<DtrdEntEvaluationRdNrs> page = new Page<>(request.getPageNum(), request.getPageSize());
//        page = this.page(page, queryWrapper);
//        return page.convert(NrsListInfo::new);
//    }

//    @Override
//    public NrsDetail getNrs(Integer dataId) {
//        DtrdEntEvaluationRdNrs dtrdEntEvaluationRdNrs = getById(dataId);
//        return Optional.ofNullable(dtrdEntEvaluationRdNrs)
//                        .map(po -> new NrsDetail().parseFromPo(po))
//                        .orElse(null);
//    }

    @Override
    public boolean saveOrUpdateNrs(NrsDetail dto) {
        DtrdEntEvaluationRdNrs dtrdEntEvaluationRdNrs = new DtrdEntEvaluationRdNrs();
        if(dto.getDataId() == null){
            dtrdEntEvaluationRdNrs = dtrdEntEvaluationRdNrs.init();
        }
        String[] diseaseStatusArray = dto.getDiseaseStatus().split(",");
        int score = 0;
        for(String str: diseaseStatusArray){
            Integer i ;
            try {
                i = Integer.parseInt(str);
            }catch (Exception e){
                e.getMessage();
                return false;
            }
            switch (i){
                case 1: score += Evaluation.NrsDiseaseStatus.BONE.getNum();break;
                case 2: score += Evaluation.NrsDiseaseStatus.ABDOMEN.getNum();break;
                case 3: score += Evaluation.NrsDiseaseStatus.CRANIUM.getNum();break;
                default:
            }
        }
        switch (dto.getNutritionalStatus()){
            case 1: break; //正常
            //3个月内体重减轻>5%或最近1个星期进食量（与需要量相比）减少20%-50%
            case 2: score += 1; break;
            //2个月内体重减轻>5%或BMI18.5-20.5或最近1个星期进食量（与需要量相比）减少50%-75%
            case 3: score += 2; break;
            //1个月内体重减轻>5%（或3个月内体重减轻>15%）或BMI<18.5（或血清蛋白<35g/l）或最近1个星期进食量（与需要量相比）减少70%-100%
            case 4: score += 3; break;
            default:
        }
        //是否大于70岁  0分小于70选1； 1分大于等于70选2
        if(dto.getIsSeventy() == 2){
            score += 1;
        }
        if(score < 3){
            dtrdEntEvaluationRdNrs.setOutput(Evaluation.NRSResult.LESS_THREE.getResult());
        }else {
            dtrdEntEvaluationRdNrs.setOutput(Evaluation.NRSResult.EQUAL_OR_MORE_THREE.getResult());
        }
        dtrdEntEvaluationRdNrs.setScore(score);
        dtrdEntEvaluationRdNrs.parseFromDto(dto);
        return saveOrUpdate(dtrdEntEvaluationRdNrs);
    }

    @Override
    public boolean removeNrs(Integer dataId) {
        return removeById(dataId);
    }

    @Override
    public NrsListInfo getCurrentNrs(Integer patientId) {
        LambdaQueryWrapper<DtrdEntEvaluationRdNrs> wrapper = baseQueryWrapper()
                .eq(DtrdEntEvaluationRdNrs::getPatientId, patientId)
                .orderByDesc(DtrdEntEvaluationRdNrs::getCreateTime)
                .last("limit 1");
        DtrdEntEvaluationRdNrs dtrdEntEvaluationRdNrs = getOne(wrapper);
        NrsListInfo info = null;
        if(dtrdEntEvaluationRdNrs != null){
            info = new NrsListInfo(dtrdEntEvaluationRdNrs);
        }
         return info;
    }

    private LambdaQueryWrapper<DtrdEntEvaluationRdNrs> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdEntEvaluationRdNrs.class)
                        .eq(DtrdEntEvaluationRdNrs::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdEntEvaluationRdNrs> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdEntEvaluationRdNrs.class)
                        .eq(DtrdEntEvaluationRdNrs::getIsDel, 0);
    }
}
